package org.syh.boyangfighthealthclub.controller;

import org.syh.boyangfighthealthclub.entity.User;
import org.syh.boyangfighthealthclub.service.IUserService;
import org.syh.boyangfighthealthclub.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: Ben
 * @Date: 2021/3/11 12:22
 */
@WebServlet("/admin/selectUser")
public class SelectUser extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
      req.setCharacterEncoding("utf-8");
      res.setContentType("text/html;utf-8");
      String p=req.getParameter("page");
      IUserService iUserService=new UserServiceImpl();
        /**
         * 每页用户数
         */
      int page;
      try {
        //当前页数
        page = Integer.valueOf(p);
      } catch (NumberFormatException e) {
        page = 1;
      }
      int pageSize = 19;
      List<User> userList=iUserService.selectUsers(page,pageSize);

        /**
         * 总用户数
         */
         int totalUsers = iUserService.selectCountUsers();
        //总页数
        int totalPages = totalUsers % pageSize == 0 ? totalUsers / pageSize : totalUsers / pageSize + 1;
        //本页起始用户序号
        int beginIndex = (page - 1) * pageSize;
        //本页末尾用户序号的下一个
        int endIndex = beginIndex + pageSize;
        if (endIndex > totalUsers){
          endIndex = totalUsers;
        }
      req.setAttribute("totalPages", totalPages);
      req.setAttribute("page", page);
      req.setAttribute("userList", userList);
      req.getRequestDispatcher("../indexuser.jsp").forward(req, res);
    }
}
